// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //Server
    serverUrl: getApp().globalData.serverUrlPrefix + "/CarRental/User/Login",
    successStr: "登录成功",


    //Navigator
    registerPage: "/pages/register/register",
    forgetPassPage: '',

    //Input Check
    username: "",
    password: "",
    // Password Check Flag
    status: "",

  },

  ///Function
  //Navigator Jump (TabBar)
  switchTabPage: function (pageName) {
    console.log("Jump to page:" + pageName);
    var pagePathTail = '/pages' + '/' + pageName + '/' + pageName;
    wx: wx.switchTab({
      url: pagePathTail,
      fail: function (res) {
        console.log("SwitchTabPage Fail");
      },
    })
  },

  ///Network
  //Send to Server
  requestLogin: function (e) {
    //Check if the username and the password is given
    if (this.username == undefined || this.password == undefined) {
      this.showMessage("无效的账号/密码");
      return false;
    }


    getApp().globalData.userName = this.username;

    //Get This pointer
    var self = this;
    //post to server
    wx.request({
      url: this.data.serverUrl, //Login
      data: {
        username: this.username,
        password: this.password
      },
      header: {
        'content-type': 'application/json;' // 默认值
      },
      success: function (res) {
        self.responseFromServer(res.data);
      },
      fail: function (res) {
        self.showMessage("网络故障");
      }
    })
  },
  //Receive from Server
  responseFromServer: function (data) {
    //Show Server response
    // console.log(data[0].result)
    var response = data[0].result;
    if (response == undefined) {
      response = data;
    }
    console.log(response);

    this.showMessage(response);


    if (response == this.data.successStr) {
      //Login Success
      getApp().globalData.userID = data[0].id;
      // console.log(data[0].id);
      // this.showMessage(value.result);
      // userID 
      this.switchTabPage('home');
    }

  },

  ///Input Widget
  //Form
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value);

    this.requestLogin();

  },

  formReset: function () {
    console.log('form发生了reset事件')
  }
  ,
  //Input
  onUserInput: function (e) {
    this.username = e.detail.value;
    // console.log(e.detail.value);
  },
  onPasswordInput: function (e) {
    this.password = e.detail.value;
    // console.log(e.detail.value);
  },

  //Navigator 
  forgetPass: function () {
    this.showMessage('功能正在建设中');
  },

  showMessage: function (message) {
    wx.showToast({
      title: message,
      icon: 'none',
      duration: 2000
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})